import java.util.*;
import java.time.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;


public class CreateNote {
	private JFrame f;
	private JButton newNote;
	private JTextField title, mailT;
	private JTextArea content;
	private JLabel jl, jl2, jl3, selectDateL, selectTimeL, hourL, minL, secL, waysL, mailL;
	private JComboBox hourC, minC, secC, waysC;
	private JPanel jp1, jp2, jp3, jp4;
	private String[] hour;
	private String[] min;
	private String[] sec;
	private String ways[] = {"1.弹窗提醒", "2.邮件提醒"};
	
	DateWindow myDate;
	
	public CreateNote() {
		hour = new String[24];
		min = new String[60];
		sec = new String[60];
		
		for(int i = 0; i < 24; i++) {
			hour[i] = i+"";
		}
		for(int i = 0; i < 60; i++) {
			min[i] = i+"";
		}
		for(int i = 0; i < 60; i++) {
			sec[i] = i+"";
		}
		
		
		f = new JFrame("���Ѳ�");
		newNote = new JButton("�½�");
		newNote.addActionListener(new Lis());
		newNote.setSize(new Dimension(100, 30));
		title = new JTextField(12);
		mailT = new JTextField(12);
		content = new JTextArea(5, 12);
		jl = new JLabel("�½�����");
		jl2 = new JLabel("���⣺");
		jl3 = new JLabel("���ݣ�");
		selectDateL = new JLabel("��ѡ���������ڣ�");
		selectTimeL = new JLabel("��ѡ������ʱ�䣺");
		waysL = new JLabel("���������ѷ�ʽ��");
		hourL = new JLabel("ʱ");
		minL = new JLabel("��");
		secL = new JLabel("��");
		mailL = new JLabel("����(��ѡ):");
		hourC = new JComboBox(hour);
		minC = new JComboBox(min);
		secC = new JComboBox(sec);
		waysC = new JComboBox(ways);
		
		myDate = new DateWindow();
		
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		jp4 = new JPanel(new GridLayout(2, 3));
		
	}
	
	public void launch() {
		f.setSize(400, 800);
		f.setLayout(new GridLayout(14, 1, 10, 10));
		f.invalidate();
		f.add(jl);
		f.add(jl2);
		f.add(title);
		f.add(jl3);
		content.setLineWrap(true);
		f.add(new JScrollPane(content));

		f.add(selectDateL);

		f.add(myDate);
		f.add(selectTimeL);
		jp4.add(hourL);
		jp4.add(minL);
		jp4.add(secL);
		jp4.add(hourC);
		jp4.add(minC);
		jp4.add(secC);
		f.add(jp4);
		f.add(waysL);
		f.add(waysC);
		f.add(mailL);
		f.add(mailT);
		jp1.add(newNote);
		f.add(jp1);
		
		f.setVisible(true);
		f.setLocationRelativeTo(null);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	//String noteTitle, String content, String reminderTime, int ways, String mail, String id
	class Lis implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			if(source == newNote) {
				//��ȡд�����ݿ����Ԫ��
				String noteTitle = title.getText();
				String remindContent = content.getText();
				int index = waysC.getSelectedIndex();
				int ways = index+1;
				String mail = mailT.getText();
				String id = LoginInterface.onlineID;
				
				Date  selectDate = myDate.getDate();
				Calendar myCal = dateToCalendar(selectDate);
				int h = Integer.valueOf(hourC.getSelectedIndex()).intValue();
				int m = Integer.valueOf(minC.getSelectedIndex()).intValue();
				int s = Integer.valueOf(secC.getSelectedIndex()).intValue();
				myCal.set(Calendar.HOUR_OF_DAY, h);
				myCal.set(Calendar.MINUTE, m);
				myCal.set(Calendar.SECOND, s);
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//����24ʱ
				String dateString =  sdf.format(myCal.getTime());
				
				//�������
				System.out.println("Title = " + noteTitle +"\nContent = " + remindContent + "\nTime = " + dateString + "\nways = " + ways + "\nmail = " + mail );
				
				//д�����ݿ�
				try {
					DatabaseCtrl.close();		//���֮ǰ���ǹر�������ر�
					DatabaseCtrl.init();		//��ʼ������
					DatabaseCtrl.insertRemindData(noteTitle, remindContent, dateString, ways, mail, id);//��������
					DatabaseCtrl.close();		//�ر�����
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				JOptionPane.showMessageDialog(null, "�����ɹ�", "��ʾ!", JOptionPane.INFORMATION_MESSAGE);
				f.dispose();	//�رմ�������
				
				//���¼���������
				MainMenu myMenu = new MainMenu();
				myMenu.launch();
				
			}
			
		}
		//��date��ת��ΪCalendar��
		  public Calendar dateToCalendar(Date date) {
		        Calendar calendar = Calendar.getInstance();
		        calendar.setTime(date);
		        return calendar;
		    }
		
	}
	
	
	

	public static void main(String[] args) {
		CreateNote myNote = new CreateNote();
		myNote.launch();

	}

}
